package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.*;
import com.project.myBeans.ResponseBean;
import com.project.service.*;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WheelChen on 2017/5/9.
 * <p>
 * 招聘信息相关操作
 */
@Controller
@RequestMapping(value = "/recruitInfo")
public class RecruitInfoController extends BaseService {
    @Autowired
    RecruitInfoService recruitInfoService;

    @Autowired
    private PositionTypeService positionTypeService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private ImagesService imagesService;


    @RequestMapping(value = "/findByCondition2", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/18
     * function: 批量获取招聘信息 + 条件 + 分页
     *
     * @param recruitInfo
     * @return
     */
    public ResponseBean findBatch2(@RequestBody RecruitInfo recruitInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (recruitInfo.getPageIndex() > 0) // 存在设置分页
                recruitInfo.setPageIndex(recruitInfo.getPageIndex() - 1);
            List<RecruitInfo> recruitInfos = recruitInfoService.findByCondition(recruitInfo);
            List<Map> maps = new ArrayList<>();
            for (RecruitInfo recruitInfo1 : recruitInfos) {
                Map map = new HashMap();
                PositionType positionType;
                if (recruitInfo1.getPositiontype() != null) {
                    positionType = positionTypeService.findById(recruitInfo1.getPositiontype());
                    map.put("positionType", positionType);
                }
                if (recruitInfo1.getOrgid() != null) {
                    OrgInfo orgInfo = orgInfoService.findByid(recruitInfo1.getOrgid());
                    if (orgInfo != null)
                        map.put("orginfoFullName", orgInfo.getFullname());
                }
                map.put("recruitInfo", recruitInfo1);
                maps.add(map);
            }
            responseBean.setData(maps);
            responseBean.setTotal(recruitInfo.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查询成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
        return responseBean;
    }

    @RequestMapping(value = "/findByCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/18
     * function: 获取招聘信息表信息
     *
     * @param recruitInfo
     * @return
     */
    public ResponseBean findBatch(@RequestBody RecruitInfo recruitInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (recruitInfo.getPageIndex() > 0) // 存在设置分页
                recruitInfo.setPageIndex(recruitInfo.getPageIndex() - 1);
            List<RecruitInfo> recruitInfos = recruitInfoService.findByCondition(recruitInfo);
            responseBean.setData(recruitInfos);
            responseBean.setTotal(recruitInfo.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查询成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("查询失败");
            return responseBean;
        }
        return responseBean;
    }

    /**
     * author: WheelChen
     * date: 2017-5-24
     * function: 根据id删除招聘信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean remove(@PathVariable("id") Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            recruitInfoService.remove(id);
            responseBean.setSuccess(true);
            responseBean.setMessage("操作成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("失败");
            return responseBean;
        }
        return responseBean;
    }

    /**
     * author: WheelChen
     * date: 2017-5-30
     * function: 根据id查找招聘信息 + 学院表 + 图片表
     *
     * @param id 用户id
     * @return
     */
    @RequestMapping(value = "/find/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean find(@PathVariable("id") Integer id) {
        ResponseBean responseBean = new ResponseBean();
        JSONObject jsonObject = new JSONObject();
        try {
            RecruitInfo recruitInfo;
            if (recruitInfoService.findRecruitInfoById(id) != null) {
                recruitInfo = recruitInfoService.findRecruitInfoById(id);
                jsonObject.put("recruitInfo", recruitInfo);
                if (recruitInfo.getSchoolid() != null){
                    School school = schoolService.findOne(recruitInfo.getSchoolid());
                    jsonObject.put("school", school);
                }
                if (recruitInfo.getWorkingpic() != null){
                    Image workingPic = imagesService.findById(recruitInfo.getWorkingpic());
                    jsonObject.put("workingPic", workingPic);
                }
            }
            responseBean.setData(jsonObject);
            responseBean.setSuccess(true);
            responseBean.setMessage("操作成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("失败");
            return responseBean;
        }
        return responseBean;
    }

    /**
     * author: WheelChen
     * date: 2017-5-31
     * function: 添加招聘信息
     *
     * @param recruitInfo 招聘信息
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean add(@RequestBody RecruitInfo recruitInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            recruitInfo.setPublishdate(new java.util.Date());
            recruitInfoService.add(recruitInfo);
            responseBean.setSuccess(true);
            responseBean.setMessage("操作成功");
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("失败");
            return responseBean;
        }
    }

    /**
     * author: WheelChen
     * date: 2017-5-31
     * function: 根据id修改招聘信息
     *
     * @param recruitInfo 招聘信息
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean modify(@RequestBody RecruitInfo recruitInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            recruitInfoService.modifyRecruitInfoById(recruitInfo);
            responseBean.setSuccess(true);
            responseBean.setMessage("操作成功");
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("失败");
            return responseBean;
        }
    }


}
